const catchUtil = {
	// 设置/添加cookie
	setCookie : (name,value,{maxAge,domain,path,secure}={})=>{
		let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
		if(typeof maxAge == 'number'){
			cookieText += `; max-age=${maxAge}`
		}
		if(domain){
			cookieText += `; domain=${domain}`
		}
		if(path){
			cookieText += `; path=${path}`
		}
		if(secure){
			cookieText += `; secure=${secure}`
		}
		document.cookie = cookieText;
	},
	
	// 获取cookie
	getCookie : (name)=>{
		if(name){
			name = decodeURIComponent(name)
			const cookieList = document.cookie.split('; ')
			for(let val of cookieList){
				if(name == decodeURIComponent(val.split('=')[0])){
					return decodeURIComponent(val.split('=')[1])
				}
			}
		}
		return ''
	},
	
	// 删除cookie
	
	
	
	setLocalStorage:(key,val)=>{
		if(key && val){
			localStorage.setItem(key,JSON.stringify(val))
		}
	},
	
	getLocalStorage:(key)=>{
		const val = localStorage.getItem(key)
		if(val){
			return JSON.parse(val)
		}
	},
	
	setSessionStorage:(key,val)=>{
		if(key && val){
			sessionStorage.setItem(key,JSON.stringify(val))
		}
	},
	
	getSessionStorage:(key)=>{
		const val = sessionStorage.getItem(key)
		if(val){
			return JSON.parse(val)
		}else{
			return {}
		}
	}
}


export default catchUtil